home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / misc / macro_1_0.lha / Handler.c next >
C/C++ Source or Header  |  1992-08-07  |  3KB  |  105 lines

  1. struct handlerdata { 
  2.     struct MsgPort mymsgport;
  3.     struct Task *thistask;
  4.     short quit;
  5.     short startrec;
  6.     short stoprec;
  7.     short insert;
  8.     short clear;
  9.     short store;
  10. };
  11.  
  12. #ifdef DEBUG
  13. void Print(char *string,BOOL newline); /* Print string to _Backstdout */
  14. char *ltoh(char *string,register long val);
  15. extern char string[11];
  16. #endif
  17.  
  18. void RawInsert(long code,long qualifier);
  19. void RemoveHandler(void);
  20. void InstallHandler(void);
  21.  
  22. extern void myhandler(void);
  23. extern BPTR _Backstdout;         /* standard output when run in background */
  24. extern struct IOStdReq *inputRequestBlock;
  25. extern struct Interrupt handlerStuff;
  26. extern struct MsgPort *inputDevPort;
  27. extern struct handlerdata data;
  28.  
  29. void RemoveHandler(void)
  30. {
  31.     if (inputRequestBlock) {
  32.         if (inputRequestBlock->io_Device) {
  33.             inputRequestBlock->io_Command = IND_REMHANDLER;  /* Remove handler */
  34.             inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  35.             DoIO((struct IORequest *)inputRequestBlock);
  36.             CloseDevice((struct IORequest *)inputRequestBlock);
  37.         }
  38.         DeleteStdIO(inputRequestBlock);
  39.     }
  40.     if (inputDevPort) {
  41.         DeletePort(inputDevPort);
  42.     }
  43.     #ifdef DEBUG
  44.     Print("Handler removed",TRUE);
  45.     #endif
  46.     return;
  47. }
  48.  
  49. void InstallHandler(void)
  50. {
  51.     inputRequestBlock = NULL;
  52.     if (!(inputDevPort = CreatePort(0L, 0L))) {
  53.         RemoveHandler();
  54.     }
  55.     if (!(inputRequestBlock = CreateStdIO(inputDevPort))) {
  56.         RemoveHandler();
  57.     }
  58.     if (OpenDevice("input.device", 0L,(struct IORequest *)inputRequestBlock, 0L)) {
  59.         RemoveHandler();
  60.     }
  61.     handlerStuff.is_Node.ln_Name = "MACRO Handler";
  62.     handlerStuff.is_Data = (APTR)&data;         /* Set up for installation of */
  63.     handlerStuff.is_Code = myhandler;          /* myhandler.                 */
  64.     handlerStuff.is_Node.ln_Pri = 51;         /* Ahead of intuition         */
  65.     inputRequestBlock->io_Command = IND_ADDHANDLER;
  66.     inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  67.     DoIO((struct IORequest *)inputRequestBlock);   /* Add me. */
  68.     #ifdef DEBUG
  69.     Print("Handler installed",TRUE);
  70.     #endif
  71.     return;
  72. }
  73.  
  74. void RawInsert(long code,long qualifier) {
  75.     /* Set up an input request */
  76.     struct InputEvent MyNewEvent;
  77.     inputRequestBlock->io_Command = IND_WRITEEVENT;
  78.     inputRequestBlock->io_Flags   = 0L;
  79.     inputRequestBlock->io_Length  = (long)sizeof(struct InputEvent);
  80.     inputRequestBlock->io_Data    = (APTR)&MyNewEvent;
  81.     MyNewEvent.ie_Class = IECLASS_RAWKEY; 
  82.     MyNewEvent.ie_Code = code;
  83.     MyNewEvent.ie_Qualifier = qualifier;
  84.     DoIO((struct IORequest *)inputRequestBlock);
  85. }
  86. #ifdef DEBUG
  87. void Print(char *string,BOOL newline) {
  88.     Write(_Backstdout,string,strlen (string));
  89.     if (newline) Write(_Backstdout,"\n",1);
  90. }
  91.  
  92. char *ltoh(char *string,register long val)
  93. {
  94.     char hex[17] = "0123456789ABCDEF";
  95.     register long count = 9;
  96.   string = "0x00000000";
  97.     while(count>1) {
  98.         string[count] = hex[val%16];
  99.         val >>= 4;
  100.         count--;
  101.     }
  102.     return(string);
  103. }
  104. #endif
  105.